package ControlLayer; //this class is in the control layer
import java.util.ArrayList; //imports the arraylist class
import ModelLayer.*;  //connects us to the model layer
import java.text.SimpleDateFormat;  // import simpleDateFormat-class. Can be used to format date
import java.util.Calendar;  //import the calendar-class. Can be used to find date and time


/**
 * Write a description of class OrderCtr here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class OrderCtr
{
    // instance variables
    private OrderContainer orderList;
    private ProductCtr pCtr;
    private CustomerCtr cCtr;
    private int counter;
    private String timeStamp;
    private int orderId;
       
    public OrderCtr()  //constructor to initialize some instance variables
    {
        orderList = OrderContainer.getInstance();
        
        pCtr = new ProductCtr();
        cCtr = new CustomerCtr();
        
        String timeStampNow = new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime());  //Makes us a string with year-month-day, made by using the simpleDateFormat and calendar to get the date.
        counter = 1;
    }
        
    public void newOrder()  //creates new order
    {
        //run the method orderId()
        orderId();
        
        //add orderId to order
        Order newOrder = new Order(orderId);
        orderList.addOrder(newOrder);
    }
    
    private int orderId() //creates the orderId for the order. Is used in the newOrdeR() method.
    {
        String timeStampNow = new SimpleDateFormat("yyMMdd").format(Calendar.getInstance().getTime());
        
        if(timeStamp.equals(timeStampNow))
        {
            orderId = Integer.parseInt(timeStamp)+counter;
        }
        else
        {
            counter = 1;
            String timeStamp = new SimpleDateFormat("yyMMdd").format(Calendar.getInstance().getTime());
            orderId = Integer.parseInt(timeStampNow)+counter;
        }
        counter++;
         
        return orderId;
    }
    
    public void addCustomer(String name, String code)  //used to add a customer to the order. Calls a method in customer controller
    {        
        Customer c = cCtr.findCustomer(name, code);
        orderList.findOrder(orderId).addCustomer(c);
    }
    
    public void createCustomer(String name, String code, String address, int paymentInfo, String email, int tlf)  //used to create a new customer. Calls a method in customer controller
    {
        cCtr.addCustomer(name, code, address, paymentInfo, email, tlf);
    }
    
    public void findProduct(String productName) //used to find a product. Calls a method in the product controller
    {
        pCtr.findProduct(productName);
    }
    
    public void getProduct(int productId, int amount) //used to get a product. Calls a method in the product controller
    {
        newOrder();
        PartOrder pO = pCtr.getProduct(productId, amount);
        
        if(pO == null)
        {
        }
        else
        {
            orderList.findOrder(orderId).addPartOrder(pO);
        }
    }
    
    public void showOrder()  //used to show the order
    {
        orderList.findOrder(orderId).printAll();
    }
    
    public void checkOut()  //used to check out and show a faktura
    {
        orderList.findOrder(orderId).checkOut();
        System.out.println("************ Faktura ************");
        showOrder();
        orderList.findOrder(orderId).getCustomer().printAll();
        
    }
}
